import configparser
import logging
import threading

from flask import Flask
from flask_migrate import Migrate
from camera import camera_bp
from swagger import swaggerui_bp
from config.database import db
from task.scheduler import configure_scheduler
# from task.scheduler import start_scheduler
from websocket.websocket_server import start_websocket_server
from flask_cors import CORS

# 读取配置文件
config = configparser.ConfigParser()
config.read('config/config.ini', encoding='utf-8')

# 配置日志记录
log_level = config['logging']['LOG_LEVEL']
logging.basicConfig(level=getattr(logging, log_level.upper(), logging.INFO))


# 移入数据迁移脚本
from utils.migration import *

# 配置日志记录
from config.log import handler
app = Flask(__name__)

CORS(app)

app.config['SQLALCHEMY_DATABASE_URI'] = config['database']['SQLALCHEMY_DATABASE_URI']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = config['database']['SQLALCHEMY_TRACK_MODIFICATIONS']

# 初始化数据库和迁移
db.init_app(app)
migrate = Migrate(app, db)

app.register_blueprint(camera_bp)
app.register_blueprint(swaggerui_bp)

# 配置 APScheduler
configure_scheduler(app)

# 启动WebSocket服务器
websocket_thread = threading.Thread(target=start_websocket_server)
websocket_thread.daemon = True
websocket_thread.start()


if __name__ == 'app':
    logging.info("flask started")


